/*
package study.HL7;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.ExtraComponents;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Type;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v23.segment.MSH;
import ca.uhn.hl7v2.model.v26.datatype.CX;
import ca.uhn.hl7v2.model.v26.datatype.NA;
import ca.uhn.hl7v2.model.v26.datatype.XPN;
import ca.uhn.hl7v2.model.v26.group.ORU_R01_OBSERVATION;
import ca.uhn.hl7v2.model.v26.group.ORU_R01_ORDER_OBSERVATION;
import ca.uhn.hl7v2.model.v26.group.ORU_R01_PATIENT;
import ca.uhn.hl7v2.model.v26.group.ORU_R01_PATIENT_RESULT;
import ca.uhn.hl7v2.model.v26.segment.MSH;
import ca.uhn.hl7v2.model.v26.segment.OBR;
import ca.uhn.hl7v2.model.v26.segment.OBX;
import ca.uhn.hl7v2.model.v26.segment.PV1;
import ca.uhn.hl7v2.protocol.ReceivingApplication;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import study.HL7.entity.JianHuYiView;
import study.HL7.entity.OutCarDao;
import study.HL7.entity.OutCarVO;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

*/
/**
 * 接收到hl7消息，业务处理逻辑类
 *//*

@Slf4j
public class HL7Business implements ReceivingApplication {

    @Autowired
    private OutCarDao mOutCarDao;
    @Autowired
    private LifeSignService lifeSignService;

    private Gson mGson = new GsonBuilder().serializeNulls().create();

    public static List<JianHuYiListView> mJianHuYiListView = new ArrayList<>();
    public static Map<String, Boolean> mLifeSignDoneMap = new HashMap<>();//标识监护仪体征信息是否采集过，true为采集过

    @Override
    public Message processMessage(Message theMessage, Map theMetadata) {
        Message response = null;
        try {
            //判断进入何种解析模式
            this.analysisModule_1(theMessage);
            response = theMessage.generateACK();
        } catch (Exception e) {
            log.info("hl7协议数据解析失败！" + e.getMessage());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return response;
    }

    */
/**
     * 利用messge转对象模式解析协议
     * MSH|^~\&|MINDRAY_D-SERIES^00A037009A00CB60^EUI-64||||20231024152606000||ORU^R01^ORU_R01|411|P|2.6|||AL|NE||UNICODE UTF-8|||IHE_PCD_001^IHE PCD^1.3.6.1.4.1.19376.1.6.1.1.1^ISO
     * PID|||^^^Hospital^PI||^^^^^^L
     * PV1||I
     * OBR|1||411^MINDRAY_D-SERIES^00A037009A00CB60^EUI-64|CONTINUOUS WAVEFORM|||20231024152605710|20231024152606710
     * OBX|1|NA|150452^MDC_PULS_OXIM_PLETH^MDC|1.3.1.150452|94^94^92^83^78^72^67^63^60^57^56^54^53^52^51^49^48^46^44^42^40^38^36^35^33^31^27^27^24^21^18^16^15^16^20^27^37^49^61^72^80^86^89^89^86^83^78^73^68^64^60^55^54^54^54^54^54^53^52^50|262656^MDC_DIM_DIMLESS^MDC
     * OBX|2|NM|0^MDC_ATTR_SAMP_RATE^MDC|1.3.1.150452.1|60|264608^MDC_DIM_PER_SEC^MDC
     * OBX|3|NM|2327^MDC_ATTR_NU_MSMT_RES^MDC|1.3.1.150452.2|1.000000|262656^MDC_DIM_DIMLESS^MDC
     * OBX|4|NM|262196^MDC_EVT_INOP^MDC|1.3.1.150452.3|32767
     *//*

    public void analysisModule_1(Message hapiMsg) throws HL7Exception, ParseException {
        log.info(hapiMsg.toString());

        JianHuYiView tJianHuYiView = new JianHuYiView();
        //1、MSH
        MSH msh = (MSH) hapiMsg.get("MSH");
        String tSeries_no = msh.getMsh3_SendingApplication().getHd2_UniversalID().getValue();
        if (StringUtils.isBlank(tSeries_no)) {
            log.error("设备号获取为空！");
            return;
        }
        String value7 = msh.getMsh7_DateTimeOfMessage().getValue();
        if (StringUtils.isNotBlank(value7)) {
            String tTime = (value7.length() > 17) ? value7.substring(0, 17) : value7;
            Date tDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").parse(tTime);
            tJianHuYiView.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tDate));
        }

        //2、PATIENT_RESULT
        ORU_R01_PATIENT_RESULT ORU_R01_PATIENT_RESULT = (ORU_R01_PATIENT_RESULT) hapiMsg.get("PATIENT_RESULT");

        //2.1、PATIENT_RESULT——>PATIENT
        ORU_R01_PATIENT patientResult = ORU_R01_PATIENT_RESULT.getPATIENT();
        CX[] pid3PatientIdentifierList = patientResult.getPID().getPid3_PatientIdentifierList();
        XPN[] pid5PatientName = patientResult.getPID().getPid5_PatientName();
        PV1 pv1 = patientResult.getVISIT().getPV1();

        //2.2、PATIENT_RESULT——>ORDER_OBSERVATION
        ORU_R01_ORDER_OBSERVATION orderObservation = (ORU_R01_ORDER_OBSERVATION) ORU_R01_PATIENT_RESULT.get("ORDER_OBSERVATION");

        //2.2.1、PATIENT_RESULT——>ORDER_OBSERVATION——>OBR
        OBR obr = orderObservation.getOBR();

        //2.2.2、PATIENT_RESULT——>PATIENT——>ORDER_OBSERVATION——>OBSERVATIONS
        List<ORU_R01_OBSERVATION> observationAll = orderObservation.getOBSERVATIONAll();
        List<Double> tMDC_PULS_OXIM_PLETHs = new ArrayList<>();
        List<Double> tMDC_ECG_ELEC_POTL_IIs = new ArrayList<>();
        List<Double> tMDC_IMPED_TTHORs = new ArrayList<>();
        String tWaveType = "";
        for (ORU_R01_OBSERVATION tORU_R01_OBSERVATION : observationAll) {
            OBX obx = tORU_R01_OBSERVATION.getOBX();
            String tType = obx.getObx3_ObservationIdentifier().getText().getValue();
            if ("MDC_PULS_OXIM_PLETH".equals(tType)) {
                tWaveType = "PLETH";
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                NA data = (NA) obx5ObservationValue[0].getData();
                //获取前四个
                Type[] components = data.getComponents();
                for (Type s : components) {
                    if (s != null) tMDC_PULS_OXIM_PLETHs.add(Double.parseDouble(s.toString()));
                }
                //获取剩余部分
                ExtraComponents extraComponents = data.getExtraComponents();
                int num = extraComponents.numComponents();
                for (int i = 0; i < num; i++) {
                    tMDC_PULS_OXIM_PLETHs.add(Double.parseDouble(String.valueOf(extraComponents.getComponent(i).getData())));
                }
            }
            if ("MDC_ECG_ELEC_POTL_II".equals(tType)) {
                tWaveType = "ECG";
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                NA data = (NA) obx5ObservationValue[0].getData();
                //获取前四个
                Type[] components = data.getComponents();
                for (Type s : components) {
                    if (s != null) tMDC_ECG_ELEC_POTL_IIs.add(Double.parseDouble(s.toString()));
                }
                //获取剩余部分
                ExtraComponents extraComponents = data.getExtraComponents();
                int num = extraComponents.numComponents();
                for (int i = 0; i < num; i++) {
                    tMDC_ECG_ELEC_POTL_IIs.add(Double.parseDouble(String.valueOf(extraComponents.getComponent(i).getData())));
                }
            }
            if ("MDC_IMPED_TTHOR".equals(tType)) {
                tWaveType = "RESP";
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                NA data = (NA) obx5ObservationValue[0].getData();
                //获取前四个
                Type[] components = data.getComponents();
                for (Type s : components) {
                    if (s != null) tMDC_IMPED_TTHORs.add(Double.parseDouble(s.toString()));
                }
                //获取剩余部分
                ExtraComponents extraComponents = data.getExtraComponents();
                int num = extraComponents.numComponents();
                for (int i = 0; i < num; i++) {
                    tMDC_IMPED_TTHORs.add(Double.parseDouble(String.valueOf(extraComponents.getComponent(i).getData())));
                }
            }
            if ("MDC_PULS_OXIM_SAT_O2".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setSpo2_value(dataV);
            }
            if ("MDC_PULS_OXIM_PULS_RATE".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setPr_value(dataV);
            }
            if ("MDC_TTHOR_RESP_RATE".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setResp_value(dataV);
            }
            if ("MDC_ECG_HEART_RATE".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setEcg_value(dataV);
            }
            if ("MDC_PRESS_CUFF_SYS".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setSbp_value(dataV);
            }
            if ("MDC_PRESS_CUFF_MEAN".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setMean_value(dataV);
            }
            if ("MDC_PRESS_CUFF_DIA".equals(tType)) {
                Varies[] obx5ObservationValue = obx.getObx5_ObservationValue();
                String dataV = obx5ObservationValue[0].getData().toString();
                tJianHuYiView.setDbp_value(dataV);
            }
        }
        tJianHuYiView.setMdc_puls_oxim_pleth(tMDC_PULS_OXIM_PLETHs);
        tJianHuYiView.setMdc_ecg_elec_potl_ii(tMDC_ECG_ELEC_POTL_IIs);
        tJianHuYiView.setMdc_imped_tthor(tMDC_IMPED_TTHORs);

        this.pushMessage(tJianHuYiView, tSeries_no, tWaveType);
    }

    */
/**
     * 推送消息逻辑处理
     * @param tJianHuYiView
     * @param tSeries_no
     *//*

    private void pushMessage(JianHuYiView tJianHuYiView, String tSeries_no, String tWaveType) {

        //判断全局对象是否包含本次监测设备信息
        boolean tFlag = false;
        for (JianHuYiListView tJianhuyilist : mJianHuYiListView) {
            tJianhuyilist.setA(StringUtils.isBlank(tWaveType) ? "VALUE" : tWaveType);
            if (tSeries_no.equals(tJianhuyilist.getSeries_no())) {
                //保留上次数值，防止频闪
                JianHuYiView tOldView = tJianhuyilist.getJianhuyi_info();
                if (StringUtils.isBlank(tJianHuYiView.getPr_value())) {
                    tJianHuYiView.setPr_value(tOldView.getPr_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getSpo2_value())) {
                    tJianHuYiView.setSpo2_value(tOldView.getSpo2_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getSbp_value())) {
                    tJianHuYiView.setSbp_value(tOldView.getSbp_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getDbp_value())) {
                    tJianHuYiView.setDbp_value(tOldView.getDbp_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getMean_value())) {
                    tJianHuYiView.setMean_value(tOldView.getMean_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getResp_value())) {
                    tJianHuYiView.setResp_value(tOldView.getResp_value());
                }
                if (StringUtils.isBlank(tJianHuYiView.getEcg_value())) {
                    tJianHuYiView.setEcg_value(tOldView.getEcg_value());
                }
                tJianhuyilist.setJianhuyi_info(tJianHuYiView);
                tFlag = true;
            }
        }
        //判断上述循环是否拿到了设备信息，若没有则加载进去
        if (!tFlag) {
            JianHuYiListView tJianHuYiListView = new JianHuYiListView();
            OutCarVO tOutCarVO = mOutCarDao.getOutCarVoByMonitorNo(tSeries_no);
            if (tOutCarVO != null) {
                tJianHuYiListView.setCar_code(tOutCarVO.getCar_code());
                tJianHuYiListView.setSeries_no(tSeries_no);
                tJianHuYiListView.setJianhuyi_info(tJianHuYiView);
                mLifeSignDoneMap.put(tSeries_no, false);
                //新增前判断当前集合是否有重复车牌
                boolean subFlag = false;
                for (JianHuYiListView tJianhuyilist : mJianHuYiListView) {
                    if (tOutCarVO.getCar_code().equals(tJianhuyilist.getCar_code())) {
                        BeanUtils.copyProperties(tJianHuYiListView, tJianhuyilist);
                        subFlag = true;
                        break;
                    }
                }
                //判断当前集合没有重复车牌，则添加到最后
                if(!subFlag) mJianHuYiListView.add(tJianHuYiListView);
            }
        }
        //获取监护仪信息更新到体征表
        this.updateLifeSign(tJianHuYiView, tSeries_no);
        //推送监护仪信息
        HL7WebSocket.broadcast(mGson.toJson(mJianHuYiListView));
    }

    @Override
    public boolean canProcess(Message message) {
        return false;
    }
}
*/
